package cn.easybuy.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.easybuy.dao.BaseDao;
import cn.easybuy.dao.UserDao;
import cn.easybuy.entity.User;
import cn.easybuy.utils.DataSourceUtil;

public class UserDaoImpl extends BaseDao implements UserDao {

	public UserDaoImpl(Connection conn) {
		super(conn);
	}

	// ע��
	@Override
	public int registerUser(User userutil) throws SQLException {
		int result = -1;
		String sql = "INSERT INTO easybuy_user(userName,loginName,`password`,sex,identityCode,email,mobile,`type`) VALUES(?,?,?,?,?,?,?,?) ";
		Object[] params = { userutil.getUserName(), userutil.getLoginName(),
				userutil.getPassword(), userutil.getSex(),
				userutil.getIdentityCode(), userutil.getEmail(),
				userutil.getMobile(), userutil.getType() };
		result = this.executeUpdate(sql, params);
		return result;
	}

	// ��¼
	@Override
	public User loginUser(String user, String pwd) throws SQLException {
		String sql = "SELECT * FROM easybuy_user WHERE loginName=? AND `password`=?";
		User userutil = new User();
		ResultSet rs = super.executeQuery(sql, user, pwd);
		if (rs != null) {
			try {
				if (rs.next()) {
					userutil.setId(rs.getInt("id"));
					userutil.setUserName(rs.getString("userName"));
					userutil.setLoginName(rs.getString("loginName"));
					userutil.setPassword(rs.getString("password"));
					userutil.setSex(rs.getInt("sex"));
					userutil.setIdentityCode(rs.getString("identityCode"));
					userutil.setEmail(rs.getString("email"));
					userutil.setMobile(rs.getString("mobile"));
					userutil.setType(rs.getInt("type"));
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				DataSourceUtil.closeAll(null, null, rs);
			}
		}
		return userutil;
	}

	@Override
	public int loginNameNum(String loginName) {
		String sql = "SELECT  COUNT(1) FROM easybuy_user WHERE  loginName=?";
		ResultSet rs = this.executeQuery(sql, loginName);
		int result = -1;
		try {
			if (rs != null) {
				while (rs.next()) {
					result = rs.getInt(1);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DataSourceUtil.closeAll(null, null, rs);
		}
		return result;
	}

	@Override
	public List<User> selectAll() {
		String sql = "select * from easybuy_user";
		List<User> userList = new ArrayList<User>();
		ResultSet rs = this.executeQuery(sql, null);
		try {
			while (rs.next()) {
				User user = new User();
				user.setId(rs.getInt("id"));
				user.setLoginName(rs.getString("loginName"));
				user.setUserName(rs.getString("userName"));
				user.setPassword(rs.getString("password"));
				user.setIdentityCode(rs.getString("identityCode"));
				user.setMobile(rs.getString("mobile"));
				user.setSex(rs.getInt("sex"));
				user.setType(rs.getInt("type"));
				userList.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DataSourceUtil.closeAll(null, null, rs);
		}
		return userList;
	}

	@Override
	public int delUser(int id) {
		String sql = "delete from easybuy_user where id=?";
		Object[] params = { id };
		int result = this.executeUpdate(sql, params);
		return result;
	}

	@Override
	public int updateUser(User user) {
		String sql = "update easybuy_user set loginName=?,userName=?,identityCode=?,mobile=?,email=?,type=? where id=?";
		Object[] params = { user.getLoginName(), user.getUserName(),
				user.getIdentityCode(), user.getMobile(), user.getEmail(),
				user.getType(), user.getId() };
		int result = this.executeUpdate(sql, params);
		return result;
	}

	@Override
	public int addUser(User user) {
		String sql = "INSERT INTO easybuy_user(userName,loginName,`password`,sex,identityCode,email,mobile,`type`) VALUE(?,?,?,?,?,?,?,?)";
		Object[] params = { user.getUserName(), user.getLoginName(),
				user.getPassword(), user.getSex(), user.getIdentityCode(),
				user.getEmail(), user.getMobile(), user.getType() };
		int result = this.executeUpdate(sql, params);
		return result;
	}

	@Override
	public List<User> selectUser(int id) {
		String sql = "select * from easybuy_user where id=?";
		List<User> userList = new ArrayList<User>();
		ResultSet rs = this.executeQuery(sql, id);
		try {
			while (rs.next()) {
				User user = new User();
				user.setId(rs.getInt("id"));
				user.setLoginName(rs.getString("loginName"));
				user.setUserName(rs.getString("userName"));
				user.setPassword(rs.getString("password"));
				user.setIdentityCode(rs.getString("identityCode"));
				user.setEmail(rs.getString("email"));
				user.setMobile(rs.getString("mobile"));
				user.setSex(rs.getInt("sex"));
				user.setType(rs.getInt("type"));
				userList.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DataSourceUtil.closeAll(null, null, rs);
		}
		return userList;
	}

	@Override
	public List<User> selectUser(String name) {
		StringBuffer sql = new StringBuffer(
				"select * from easybuy_user where 1=1");
		if (name != null && name != "") {
			String na = " and userName like '%" + name + "%'";
			sql.append(na);
		}
		List<User> userList = new ArrayList<User>();
		ResultSet rs = this.executeQuery(sql.toString(), null);
		try {
			while (rs.next()) {
				User user = new User();
				user.setId(rs.getInt("id"));
				user.setLoginName(rs.getString("loginName"));
				user.setUserName(rs.getString("userName"));
				user.setPassword(rs.getString("password"));
				user.setIdentityCode(rs.getString("identityCode"));
				user.setEmail(rs.getString("email"));
				user.setMobile(rs.getString("mobile"));
				user.setSex(rs.getInt("sex"));
				user.setType(rs.getInt("type"));
				userList.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DataSourceUtil.closeAll(null, null, rs);
		}
		return userList;
	}

	@Override
	public List<User> selectUserInfo(String name) {
		String sql = "select * from easybuy_user where loginName=?";
		List<User> userList = new ArrayList<User>();
		ResultSet rs = this.executeQuery(sql.toString(), name);
		try {
			while (rs.next()) {
				User user = new User();
				user.setId(rs.getInt("id"));
				user.setLoginName(rs.getString("loginName"));
				user.setUserName(rs.getString("userName"));
				user.setPassword(rs.getString("password"));
				user.setIdentityCode(rs.getString("identityCode"));
				user.setEmail(rs.getString("email"));
				user.setMobile(rs.getString("mobile"));
				user.setSex(rs.getInt("sex"));
				user.setType(rs.getInt("type"));
				userList.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DataSourceUtil.closeAll(null, null, rs);
		}
		return userList;
	}

	@Override
	public List<User> selectUserInfo2(String name) {
		String sql = "SELECT u.id,u.userName,u.password,u.identityCode,u.email,u.mobile,u.type,o.address FROM easybuy_user u,easybuy_user_address o WHERE u.id=o.userId AND u.loginName=?";
		List<User> userList = new ArrayList<User>();
		ResultSet rs = this.executeQuery(sql.toString(), name);
		try {
			while (rs.next()) {
				User user = new User();
				user.setId(rs.getInt("id"));
				user.setUserName(rs.getString("userName"));
				user.setPassword(rs.getString("password"));
				user.setIdentityCode(rs.getString("identityCode"));
				user.setEmail(rs.getString("email"));
				user.setMobile(rs.getString("mobile"));
				user.setType(rs.getInt("type"));
				user.setUserAddress(rs.getString("address"));
				userList.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DataSourceUtil.closeAll(null, null, rs);
		}
		return userList;
	}
	
	@Override
	public int updatePwd(int id, String password) {
		String sql = "UPDATE easybuy_user SET PASSWORD=? WHERE id=?";
		Object[] params={password,id};
		int result=this.executeUpdate(sql, params);
		return result;
	}
}
